home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / dev / gui / BGUI11c.lha / demos / colorwheel.c < prev    next >
C/C++ Source or Header  |  1994-11-02  |  31KB  |  429 lines

  1. ;/* Execute me to compile with DICE V3.0
  2. dcc colorwheel.c -proto -mi -ms -mRR -3.0 -lbgui
  3. quit
  4. */
  5. /*
  6. **         $RCSfile: colorwheel.c,v $
  7. **      Description: Simple BGUI external class demonstration.
  8. **        Copyright: (C) Copyright 1994 Jaba Development.
  9. **                   (C) Copyright 1994 Jan van den Baard.
  10. **                   All Rights Reserved.
  11. **
  12. **          $Author: jaba $
  13. **        $Revision: 1.6 $
  14. **            $Date: 1994/10/23 14:51:35 $
  15. **/
  16.  
  17. /*
  18. **      This example is based on the original ColorWheel.c
  19. **      code by Commodore.
  20. **/
  21.  
  22. #include <exec/types.h>
  23. #include <exec/libraries.h>
  24. #include <libraries/gadtools.h>
  25. #include <dos/dos.h>
  26. #include <gadgets/colorwheel.h>
  27. #include <gadgets/gradientslider.h>
  28. #include <graphics/displayinfo.h>
  29. #include <libraries/bgui.h>
  30. #include <libraries/bgui_macros.h>
  31.  
  32. #include <clib/alib_protos.h>
  33.  
  34. #include <proto/exec.h>
  35. #include <proto/intuition.h>
  36. #include <proto/bgui.h>
  37. #include <proto/graphics.h>
  38.  
  39. #include <clib/colorwheel_protos.h>
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43.  
  44. /*
  45. **      Library base pointers.
  46. **/
  47. struct Library *BGUIBase = NULL, *ColorWheelBase = NULL, *GradientSliderBase = NULL;
  48.  
  49. /*
  50. **      Object ID's
  51. **/
  52. #define ID_QUIT                 1
  53. #define ID_WHEEL                2
  54.  
  55. /*
  56. **      Structure for LoadRGB32().
  57. **/
  58. struct load32 {
  59.         UWORD           l32_len;
  60.         UWORD           l32_pen;
  61.         ULONG           l32_red;
  62.         ULONG           l32_grn;
  63.         ULONG           l32_blu;
  64. };
  65.  
  66. /*
  67. **      Ensure correct operation on ECS.
  68. **/
  69. #define GRADCOLORS 4
  70.  
  71. /*
  72. **      The program even does version checking... sjeez...
  73. **/
  74. extern struct Library *SysBase;
  75.  
  76. /*
  77. **      And were in...
  78. **/
  79. int main( int argc, char *argv[] )
  80. {
  81.         Object                  *WN_Window, *GA_Quit, *GA_Wheel, *GA_GrSlider, *GA_Master, *RealSlider = NULL;
  82.         struct Window           *win;
  83.         struct Screen           *scr;
  84.         ULONG                   winsig = 0L, sigrec, rc;
  85.         BOOL                    running = TRUE;
  86.         ULONG                   colortable[ 96 ];
  87.         struct load32           color_list[ GRADCOLORS + 1 ];
  88.         WORD                    penns[ GRADCOLORS + 1 ];
  89.         struct ColorWheelRGB    rgb;
  90.         struct ColorWheelHSB    hsb;
  91.         ULONG                   modeID = HIRES_KEY;
  92.         UWORD                   numPens, i;
  93.         DisplayInfoHandle       displayhandle;
  94.         struct DimensionInfo    dimensioninfo;
  95.  
  96.         /*
  97.         **      Need OS 3.0.
  98.         **/
  99.         if ( SysBase->lib_Version < 39 ) {
  100.                 puts( "OS 3.0 required!" );
  101.                 exit( 0 );
  102.         }
  103.  
  104.         /*
  105.         **      Open the libraries. Get display information etc...
  106.         **/
  107.         if ( BGUIBase = OpenLibrary( BGUINAME, BGUIVERSION )) {
  108.                 if ( ColorWheelBase = OpenLibrary( "gadgets/colorwheel.gadget", 39L )) {
  109.                         if ( GradientSliderBase = OpenLibrary( "gadgets/gradientslider.gadget", 39L )) {
  110.                                 if ( displayhandle = FindDisplayInfo( modeID )) {
  111.                                         if ( GetDisplayInfoData( displayhandle, ( UBYTE * )&dimensioninfo, sizeof( struct DimensionInfo ), DTAG_DIMS, NULL )) {
  112.                                                 /*
  113.                                                 **      Open up a screen with as many
  114.                                                 **      colors as possible. Also make it
  115.                                                 **      like your workbench screen.
  116.                                                 **/
  117.                                                 if ( scr = OpenScreenTags( NULL, SA_Depth,              dimensioninfo.MaxDepth,
  118.                                                                                  SA_SharePens,          TRUE,
  119.                                                                                  SA_LikeWorkbench,      TRUE,
  120.                                                                                  SA_Interleaved,        TRUE,
  121.                                                                                  SA_Title,              "ColorWheel Screen",
  122.                                                                                  TAG_END )) {
  123.                                                         /*
  124.                                                         **      Get the colors.
  125.                                                         **/
  126.                                                         GetRGB32( scr->ViewPort.ColorMap, 0L, 32L, colortable );
  127.  
  128.                                                         /*
  129.                                                         **      Setup gradient slider
  130.                                                         **      as color 0.
  131.                                                         **/
  132.                                                         rgb.cw_Red   = colortable[ 0 ];
  133.                                                         rgb.cw_Green = colortable[ 1 ];
  134.                                                         rgb.cw_Blue  = colortable[ 2 ];
  135.  
  136.                                                         /*
  137.                                                         **      Convert the RGB values to
  138.                                                         **      HSB values.
  139.                                                         **/
  140.                                                         ConvertRGBToHSB( &rgb, &hsb );
  141.  
  142.                                                         /*
  143.                                                         **      Maximum brightness.
  144.                                                         **/
  145.                                                         hsb.cw_Brightness = 0xffffffff;
  146.  
  147.                                                         numPens = 0;
  148.  
  149.                                                         /*
  150.                                                         **      Build the pen array for the
  151.                                                         **      gradient slider (4 pens).
  152.                                                         **/
  153.                                                         while ( numPens < GRADCOLORS ) {
  154.                                                                 /*
  155.                                                                 **      Compute dim level of this pen.
  156.                                                                 **/
  157.                                                                 hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / GRADCOLORS ) * numPens );
  158.                                                                 /*
  159.                                                                 **      Convert this dim level
  160.                                                                 **      to RGB values.
  161.                                                                 **/
  162.                                                                 ConvertHSBToRGB( &hsb, &rgb );
  163.                                                                 /*
  164.                                                                 **      Allocate a pen.
  165.                                                                 **/
  166.                                                                 if (( penns[ numPens ] = ObtainPen( scr->ViewPort.ColorMap, -1, rgb.cw_Red, rgb.cw_Green, rgb.cw_Blue, PEN_EXCLUSIVE )) == -1 )
  167.                                                                         break;
  168.                                                                 /*
  169.                                                                 **      Stuff it in the color list.
  170.                                                                 **/
  171.                                                                 color_list[ numPens ].l32_len = 1;
  172.                                                                 color_list[ numPens ].l32_pen = penns[ numPens ];
  173.                                                                 /*
  174.                                                                 **      Next...
  175.                                                                 **/
  176.                                                                 numPens++;
  177.                                                         }
  178.  
  179.                                                         /*
  180.                                                         **      Terminate pen array and
  181.                                                         **      color list.
  182.                                                         **/
  183.                                                         penns[ numPens ]              = ~0;
  184.                                                         color_list[ numPens ].l32_len = 0;
  185.  
  186.                                                         /*
  187.                                                         **      Create the gradient slider here
  188.                                                         **      _with_ the EXT_NoRebuild tag set
  189.                                                         **      to TRUE. This _must_ be set to
  190.                                                         **      TRUE otherwise the external class
  191.                                                         **      will rebuild the object after each
  192.                                                         **      re-size resulting in colourful
  193.                                                         **      crashes when it gets attached to
  194.                                                         **      the colorwheel :)
  195.                                                         **/
  196.                                                         GA_GrSlider = ExternalObject,
  197.                                                                 EXT_MinWidth,           10,
  198.                                                                 EXT_MinHeight,          10,
  199.                                                                 EXT_ClassID,            "gradientslider.gadget",
  200.                                                                 EXT_NoRebuild,          TRUE,
  201.                                                                 GRAD_PenArray,          penns,
  202.                                                                 PGA_Freedom,            LORIENT_VERT,
  203.                                                                 TAG_END );
  204.  
  205.                                                         /*
  206.                                                         **      Pick up a pointer to the
  207.                                                         **      "real" gradient slider object.
  208.                                                         **      This will be the pointer we
  209.                                                         **      pass to the colorwheel.
  210.                                                         **/
  211.                                                         if ( GA_GrSlider )
  212.                                                                 GetAttr( EXT_Object, GA_GrSlider, ( ULONG * )&RealSlider );
  213.  
  214.                                                         /*
  215.                                                         **      Create a small window.
  216.                                                         **/
  217.                                                         WN_Window = WindowObject,
  218.                                                                 WINDOW_Title,           "ColorWheel",
  219.                                                                 WINDOW_RMBTrap,         TRUE,
  220.                                                                 WINDOW_Screen,          scr,
  221.                                                                 WINDOW_ScaleWidth,      20,
  222.                                                                 WINDOW_ScaleHeight,     20,
  223.                                                                 /*
  224.                                                                 **      The colorwheel is really slow :(
  225.                                                                 **/
  226.                                                                 WINDOW_NoBufferRP,      TRUE,
  227.                                                                 WINDOW_MasterGroup,
  228.                                                                         GA_Master = VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
  229.                                                                                 StartMember, TitleSeperator( "Wheel & Slider" ), EndMember,
  230.                                                                                 StartMember,
  231.                                                                                         HGroupObject, Spacing( 4 ),
  232.                                                                                                 StartMember,
  233.                                                                                                         /*
  234.                                                                                                         **      The EXT_NoRebuild tag may _not_ be set to
  235.                                                                                                         **      TRUE for a colorwheel object. This is due
  236.                                                                                                         **      to the fact that colorwheels cannot change
  237.                                                                                                         **      size. This is also the reason why we need
  238.                                                                                                         **      to track the attributes of the colorwheel.
  239.                                                                                                         **/
  240.                                                                                                         GA_Wheel = ExternalObject,
  241.                                                                                                                 EXT_MinWidth,           30,
  242.                                                                                                                 EXT_MinHeight,          30,
  243.                                                                                                                 EXT_ClassID,            "colorwheel.gadget",
  244.                                                                                                                 WHEEL_Screen,           scr,
  245.                                                                                                                 /*
  246.                                                                                                                 **      Pass a pointer to the "real" gradient slider
  247.                                                                                                                 **      here.
  248.                                                                                                                 **/
  249.                                                                                                                 WHEEL_GradientSlider,   RealSlider,
  250.                                                                                                                 WHEEL_Red,              colortable[ 0 ],
  251.                                                                                                                 WHEEL_Green,            colortable[ 1 ],
  252.                                                                                                                 WHEEL_Blue,             colortable[ 2 ],
  253.                                                                                                                 GA_FollowMouse,         TRUE,
  254.                                                                                                                 GA_ID,                  ID_WHEEL,
  255.                                                                                                                 /*
  256.                                                                                                                 **      These attributes of the colorwheel are
  257.                                                                                                                 **      tracked and reset to the object after
  258.                                                                                                                 **      it has been rebuild. This way the current
  259.                                                                                                                 **      colorwheel internals will not be lost
  260.                                                                                                                 **      after the object is re-build.
  261.                                                                                                                 **/
  262.                                                                                                                 EXT_TrackAttr,          WHEEL_Red,
  263.                                                                                                                 EXT_TrackAttr,          WHEEL_Green,
  264.                                                                                                                 EXT_TrackAttr,          WHEEL_Blue,
  265.                                                                                                                 EXT_TrackAttr,          WHEEL_Hue,
  266.                                                                                                                 EXT_TrackAttr,          WHEEL_Saturation,
  267.                                                                                                                 EXT_TrackAttr,          WHEEL_Brightness,
  268.                                                                                                         EndObject,
  269.                                                                                                 EndMember,
  270.                                                                                                 /*
  271.                                                                                                 **      Add the externalclass object of the
  272.                                                                                                 **      gradient slider here. Right next to
  273.                                                                                                 **      the colorwheel :)
  274.                                                                                                 **/
  275.                                                                                                 StartMember,
  276.                                                                                                         GA_GrSlider, FixWidth( 20 ),
  277.                                                                                                 EndMember,
  278.                                                                                         EndObject,
  279.                                                                                 EndMember,
  280.                                                                                 StartMember, HorizSeperator, EndMember,
  281.                                                                                 StartMember,
  282.                                                                                         HGroupObject,
  283.                                                                                                 VarSpace( DEFAULT_WEIGHT ),
  284.                                                                                                 StartMember, GA_Quit = KeyButton( "_Quit", ID_QUIT ), EndMember,
  285.                                                                                                 VarSpace( DEFAULT_WEIGHT ),
  286.                                                                                         EndObject, FixMinHeight,
  287.                                                                                 EndMember,
  288.                                                                         EndObject,
  289.                                                         EndObject;
  290.  
  291.                                                         if ( WN_Window ) {
  292.                                                                 /*
  293.                                                                 **      Make button selectable by the keyboard.
  294.                                                                 **/
  295.                                                                 GadgetKey( WN_Window, GA_Quit, "q" );
  296.                                                                 /*
  297.                                                                 **      Open up the window.
  298.                                                                 **/
  299.                                                                 if ( win = WindowOpen( WN_Window )) {
  300.                                                                         /*
  301.                                                                         **      Obtain window sigmask.
  302.                                                                         **/
  303.                                                                         GetAttr( WINDOW_SigMask, WN_Window, &winsig );
  304.                                                                         /*
  305.                                                                         **      Wait for messages.
  306.                                                                         **/
  307.                                                                         do {
  308.                                                                                 sigrec = Wait( winsig );
  309.  
  310.                                                                                 /*
  311.                                                                                 **      Window signal?
  312.                                                                                 **/
  313.                                                                                 if ( sigrec & winsig ) {
  314.                                                                                         while ( WN_Window && (( rc = HandleEvent( WN_Window )) != WMHI_NOMORE )) {
  315.                                                                                                 switch ( rc ) {
  316.  
  317.                                                                                                         case    WMHI_CLOSEWINDOW:
  318.                                                                                                         case    ID_QUIT:
  319.                                                                                                                 /*
  320.                                                                                                                 **      The end.
  321.                                                                                                                 **/
  322.                                                                                                                 running = FALSE;
  323.                                                                                                                 break;
  324.  
  325.                                                                                                         case    ID_WHEEL:
  326.                                                                                                                 /*
  327.                                                                                                                 **      Obtain the wheel it's HSB settings.
  328.                                                                                                                 **/
  329.                                                                                                                 GetAttr( WHEEL_HSB, GA_Wheel, ( ULONG * )&hsb );
  330.  
  331.                                                                                                                 i = 0;
  332.  
  333.                                                                                                                 /*
  334.                                                                                                                 **      Recompute the gradient slider
  335.                                                                                                                 **      dim colors for the current
  336.                                                                                                                 **      colorwheel setting.
  337.                                                                                                                 **/
  338.                                                                                                                 while ( i < numPens ) {
  339.                                                                                                                         /*
  340.                                                                                                                         **      Compute dim level of this pen.
  341.                                                                                                                         **/
  342.                                                                                                                         hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / numPens ) * i );
  343.                                                                                                                         /*
  344.                                                                                                                         **      Convert to RGB values.
  345.                                                                                                                         **/
  346.                                                                                                                         ConvertHSBToRGB(&hsb,&rgb);
  347.                                                                                                                         /*
  348.                                                                                                                         **      Setup the pens in the color list.
  349.                                                                                                                         **/
  350.                                                                                                                         color_list[ i ].l32_red = rgb.cw_Red;
  351.                                                                                                                         color_list[ i ].l32_grn = rgb.cw_Green;
  352.                                                                                                                         color_list[ i ].l32_blu = rgb.cw_Blue;
  353.                                                                                                                         /*
  354.                                                                                                                         **      Next...
  355.                                                                                                                         **/
  356.                                                                                                                         i++;
  357.                                                                                                                 }
  358.                                                                                                                 /*
  359.                                                                                                                 **      Show the changes in the gradient slider.
  360.                                                                                                                 **/
  361.                                                                                                                 LoadRGB32( &scr->ViewPort, ( ULONG * )color_list );
  362.                                                                                                                 break;
  363.                                                                                                 }
  364.                                                                                         }
  365.                                                                                 }
  366.                                                                         } while ( running );
  367.                                                                 }
  368.                                                                 /*
  369.                                                                 **      Kill the window.
  370.                                                                 **/
  371.                                                                 DisposeObject( WN_Window );
  372.                                                         }
  373.                                                         /*
  374.                                                         **      Release the allocated pens.
  375.                                                         **/
  376.                                                         while ( numPens > 0 ) {
  377.                                                                 numPens--;
  378.                                                                 ReleasePen( scr->ViewPort.ColorMap, penns[ numPens ] );
  379.                                                         }
  380.                                                         /*
  381.                                                         **      Close the screen.
  382.                                                         **/
  383.                                                         CloseScreen( scr );
  384.                                                 }
  385.                                         }
  386.                                 }
  387.                                 /*
  388.                                 **      Close the gradient.gadget.
  389.                                 **/
  390.                                 CloseLibrary( GradientSliderBase );
  391.                         }
  392.                         /*
  393.                         **      Close the colorwheel.gadget.
  394.                         **/
  395.                         CloseLibrary( ColorWheelBase );
  396.                 }
  397.                 /*
  398.                 **      And the bgui.library.
  399.                 **/
  400.                 CloseLibrary( BGUIBase );
  401.         }
  402. }
  403.  
  404. #ifdef _DCC
  405. int wbmain( struct wbStartup *wbs )
  406. {
  407.         return( main( 0, NULL ));
  408. }
  409. #endif
  410.  
  411. /*
  412.  *      $Log: colorwheel.c,v $
  413.  * Revision 1.6  1994/10/23  14:51:35  jaba
  414.  * Added the WINDOW_NoBufferRP tag.
  415.  *
  416.  * Revision 1.5  1994/09/06  20:45:20  jaba
  417.  * Fixed up the source a bit.
  418.  *
  419.  * Revision 1.3  1994/08/03  11:18:09  jaba
  420.  * Switched to proto/ instead of clib/.
  421.  *
  422.  * Revision 1.2  1994/07/25  20:28:46  jaba
  423.  * Replaced the frame by two seperators.
  424.  *
  425.  * Revision 1.1  1994/06/20  20:57:48  jaba
  426.  * Initial revision
  427.  *
  428.  */
  429.